<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="shebeiguanli">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="设备编号">
        <el-input
          v-model.trim="formInline.innerCode"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="sbSearch"
          >查询</el-button
        >
        <el-button type="success" icon="el-icon-search" @click="sbReset"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="dw-bottom">
      <div class="dw-add">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click.native="addSb"
          >新建</el-button
        >
        <el-button @click.native="piliangAction">批量操作</el-button>
      </div>
      <!-- 列表区域 -->
      <div class="dw-content">
        <el-table
          :data="ShjList"
          style="width: 100%"
          tooltip-effect="dark"
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="80"> </el-table-column>
          <el-table-column prop="num" label="序号" width="100" type="index">
          </el-table-column>
          <el-table-column prop="innerCode" label="设备编号" width="150">
          </el-table-column>
          <el-table-column prop="type[name]" label="设备型号" width="150">
          </el-table-column>
          <el-table-column prop="node[addr]" label="详细地址" width="200">
          </el-table-column>
          <el-table-column prop="ownerName" label="合作商" width="150">
          </el-table-column>
          <el-table-column
            prop="vmStatus"
            label="设备状态"
            width="130"
            :formatter="formaAter"
          >
          </el-table-column>
          <el-table-column prop="labor" label="操作" class="caozuo">
            <template v-slot="{ row }">
              <span>货道</span>
              <span @click="clFn(row)">策略</span>
              <span @click="xgFn(row)">修改</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="dw-footer">
          <!-- 页码 -->
          <Page
            :total="Number(totalCount)"
            :totalPage="Number(totalPage)"
            :page.sync="data.pageIndex"
            @gengxin="getShjList"
          ></Page>
        </div>
        <!-- 新增 -->
        <SbglXz :sbglVisible.sync="sbglVisible" @gengxin="getShjList"></SbglXz>
      </div>
    </div>
    <!-- 批量操作弹窗 -->
    <el-dialog
      title="批量策略管理"
      :visible="plVisible"
      width="40%"
      @close="closeFn"
    >
      <span>
        <el-form>
          <el-form-item label="选择策略:" :v-model="clForm">
            <el-select
              placeholder="请选择"
              style="whitd: 80%"
              v-model="clForm.policyId"
            >
              <el-option
                :label="item.policyName"
                :value="item.policyId"
                v-for="item in clList"
                :key="item.policyId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="clOk">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 策略管理弹窗 -->
    <el-dialog
      title="策略管理"
      :visible="clglVisible"
      width="40%"
      @close="closeFn2"
      class="clgltc"
    >
      <span>
        <el-form label-position="right" label-width="80px" class="clgl">
          <el-form-item label="机器编号:">
            <p>{{ clxqObj.innerCode }}</p>
          </el-form-item>
          <el-form-item label="策略名称:">
            <p>{{ clxqObj.policyName }}</p>
          </el-form-item>
          <el-form-item label="策略方案:">
            <p>{{ clxqObj.discount }}%</p>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qxClFn">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹窗 -->
    <el-dialog title="修改设备" :visible="xgVisible" width="40%" @close='closeFn3' :model='xgForm'>
      <span>
        <el-form v-if="XGobj && XGobj.node">
          <el-form-item label="机器编号：">
            <p>{{ XGobj.innerCode}}</p>
          </el-form-item>
          <el-form-item label="供货时间：">
            <p>{{XGobj.createTime}}</p>
          </el-form-item>
          <el-form-item label="设备类型：">
            <p>{{XGobj.type.name}}</p>
          </el-form-item>
          <el-form-item label="设备容量：">
            <p>{{ XGobj.type.channelMaxCapacity}}</p>
          </el-form-item>
          <el-form-item label="设备点位：" >
            <el-select v-model='xgForm.DwName' >
              <el-option :label="item.name" :value="item.id" v-for="item in dwList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="合作商：">
            <p>{{XGobj.ownerName}}</p>
          </el-form-item>
          <el-form-item label="所属区域：">
            <p>{{XGobj.region.name}}</p>
          </el-form-item>
          <el-form-item label="设备地址：">
            <p >{{XGobj.node.addr}}</p>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn3">取 消</el-button>
        <el-button type="primary" @click="XgDwOK">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SbglXz from './components/sbglXz.vue'
import Page from '../dianweiguanli/components/Page.vue'
import { getDw } from '@/api/dianweiguanli/dianwei.js'
import {
  getShj,
  sbCl,
  sbClAll,
  ckCpCl,
  qxCl,
  xgShjDw
} from '@/api/shebeiguanli/shebeiguanli'
export default {
  components: {
    Page,
    SbglXz
  },
  data () {
    return {
      xgVisible: false,
      clglVisible: false,
      plVisible: false,
      sbglVisible: false,
      loading: false,
      hzsXqVisible: false,
      hzsShow: false,
      hzsVisible: false,
      formInline: {
        innerCode: ''
      },
      ShjList: [],
      clList: [],
      innerCodeList: [],
      innerCodeArr: [],
      dwList: [],
      XGobj: {},
      xgForm: {
        DwName: ''
      },
      clxqObj: {},
      clForm: {
        policyId: ''
      },
      data: {
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0, // 总量
      totalPage: 0 // 总页数
    }
  },
  created () {
    this.getShjList()
  },
  mounted () {},
  methods: {
    closeFn2 () {
      this.clglVisible = false
    },
    async getShjList () {
      this.loading = true
      const { data } = await getShj(this.data)
      this.ShjList = data.currentPageRecords
      // 处理详细地址
      this.ShjList.forEach((item) => {
        item.node.addr = item.node.addr
          .split('-')
          .reverse()
          .splice(0, 1)
          .join('')
      })
      this.totalCount = data.totalCount
      this.totalPage = data.totalPage
      this.loading = false
    },
    // 处理设备状态数据
    formaAter (row, column, cellValue, index) {
      // console.log('cellValue :>> ', cellValue)
      if (cellValue === 0) return '未投放'
      if (cellValue === 1) return '运营'
      if (cellValue === 3) return '撤机'
    },
    // 设备编号搜索
    async sbSearch () {
      const { data } = await getShj({ ...this.data, ...this.formInline })
      this.ShjList = data.currentPageRecords
      this.ShjList.forEach((item) => {
        item.node.addr = item.node.addr
          .split('-')
          .reverse()
          .splice(0, 1)
          .join('')
      })
      this.totalCount = data.totalCount
      this.totalPage = data.totalPage
      this.loading = false
    },
    // 重置
    sbReset () {
      this.getShjList()
      this.formInline = {
        innerCode: ''
      }
    },
    // 新增设备
    addSb () {
      this.sbglVisible = true
    },
    // 策略弹框关闭
    closeFn () {
      this.plVisible = false
    },
    // 控制策略弹框显示
    async piliang () {
      const res = await sbCl()
      this.clList = res.data
      this.plVisible = true
    },
    // 单个策略修改展示弹窗
    async clFn (row) {
      // 判断单条数据是否有策略
      const { data } = await ckCpCl(row.innerCode)
      // console.log('data.length :>> ', data.length)
      if (data.length <= 0) {
        this.innerCodeList = [row.innerCode]
        this.piliang()
      } else {
        this.clglVisible = true
        // console.log('data :>> ', data)
        this.clxqObj = data
      }
    },
    // 批量策略展示弹窗
    piliangAction () {
      this.innerCodeList = []
      // 判断表格是否有选择
      if (this.innerCodeArr.length <= 0) {
        this.$message.warning('请选择')
      } else {
        this.innerCodeArr.forEach((item) => {
          this.innerCodeList.push(item.innerCode)
        })
        this.piliang()
      }
    },
    // 表格选择
    handleSelectionChange (arr) {
      this.innerCodeArr = arr
    },
    // 策略确定
    async clOk () {
      await sbClAll({
        innerCodeList: this.innerCodeList,
        policyId: this.clForm.policyId
      })
      this.clForm = {
        policyId: ''
      }
      this.closeFn()
      this.getShjList()
    },
    // 取消策略
    async qxClFn () {
      await qxCl(this.clxqObj)
      this.closeFn2()
      this.clOk()
    },
    // 修改
    closeFn3 () {
      this.xgVisible = false
    },
    async xgFn (row) {
      console.log(row)
      this.XGobj = row
      console.log('this.XGobj :>> ', this.XGobj)
      this.xgForm.DwName = row.node.addr
      // 点位信息
      const { data } = await getDw({ pageIndex: 1, pageSize: 10000 })
      this.dwList = data.currentPageRecords
      this.xgVisible = true
    },
    // 提交修改
    async XgDwOK () {
      await xgShjDw(this.XGobj)
      this.closeFn3()
      this.getShjList()
    }
  }
}
</script>
<style lang="less" scoped>
.shebeiguanli {
  * {
    box-sizing: border-box;
  }
  padding: 80px 20px 20px;
  background-color: #f8fafd;

  .dw-bottom {
    padding-left: 17px;
    padding-top: 20px;
    width: 1185px;
    height: 673px;
    background-color: #fff;
    .dw-add {
      margin-bottom: 20px;
      width: 1153px;
      height: 36px;
      .el-button--primary {
        // display: inline-block;
        cursor: pointer;
        width: 80px;
        height: 36px;
        color: #fff;
        font-size: 14px;
        background-color: #ff6726;
        border-color: #ff6726;
        padding: 0;
      }
      .el-button--default {
        width: 80px !important;
        height: 36px;
        padding: 0;
        color: #655b56 !important;
        background-color: #fbf4f0 !important;
        border-color: #fbf4f0;
      }
    }
    .dw-content {
      /deep/.el-table th {
        background-color: #f3f6fb;
      }
      span {
        margin-right: 10px;
        cursor: pointer;
        font-size: 14px;
        color: #5f84ff;
      }
      .dw-footer {
        display: flex;
        justify-content: space-between;
        padding: 32px 16px;
        width: 1153px;
        height: 96px;
        p {
          font-size: 16px;
          color: #606266;
        }
        .btn {
          .btn1,
          .btn2 {
            // display: flex;
            background-color: #d5ddf8;
            width: 70px;
            height: 32px;
            margin: 0 16px;
            padding: 0;
          }
        }
      }
    }
  }
  /deep/.el-dialog__body {
    margin-left: 40px;
    .el-select {
      width: 350px;
    }
  }
  .clgltc {
    .clgl {
      display: flex;
      flex-wrap: wrap;
      width: 550px;
      .el-form-item {
        width: 260px;
        height: 36px;
      }
    }
    /deep/.el-dialog__footer {
      padding: 10px 20px 20px;
      text-align: center;
      box-sizing: border-box;
      .el-button--default {
        background-color: #fbf4f0 !important;
        border-color: #fbf4f0 !important;
        color: #655b56 !important;
      }
    }
  }
}
</style>
